package com.example.parkdemo.sign

import android.os.Bundle
import android.util.Log
import com.amap.api.location.AMapLocationClient
import com.amap.api.location.AMapLocationClientOption
import com.amap.api.location.AMapLocationListener
import com.amap.api.maps2d.AMap
import com.amap.api.maps2d.model.MyLocationStyle

import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.base.mvvm.BaseActivity
import com.example.base.net.Const
import com.example.parkdemo.databinding.ActivitySignBinding
import com.google.gson.Gson
import dagger.hilt.android.AndroidEntryPoint
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody


@AndroidEntryPoint
class SignActivity : BaseActivity<ActivitySignBinding, SignViewModel>() {
    private lateinit var map: AMap
    private lateinit var myLocationStyle: MyLocationStyle
    lateinit var mLocationClient: AMapLocationClient
    //声明AMapLocationClientOption对象

    //声明AMapLocationClientOption对象
    lateinit var mLocationOption: AMapLocationClientOption

    val mLocationListener = AMapLocationListener {
        if (it.errorCode == 0) {
//可在其中解析amapLocation获取相应内容。
            Log.i("TagA", "定位成功:$it")
        } else {
            //定位失败时，可通过ErrCode（错误码）信息来确定失败的原因，errInfo是错误信息，详见错误码表。
            Log.e(
                "TagA", "定位黑白，错误码："
                        + it.errorCode + ", errInfo:"
                        + it.errorInfo
            )
        }

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //获取地图控件引用
        //在activity执行onCreate时执行mMapView.onCreate(savedInstanceState)，创建地图
        binding.map.onCreate(savedInstanceState);
        map = binding.map.map
        myLocationStyle = MyLocationStyle()
        myLocationStyle.interval(2000); //设置连续定位模式下的定位间隔，只在连续定位模式下生效，单次定位模式下不会生效。单位为毫秒。
//aMap.getUiSettings().setMyLocationButtonEnabled(true);设置默认定位按钮是否显示，非必需设置。
        map.isMyLocationEnabled = true;// 设置为true表示启动显示定位蓝点，false表示隐藏定位蓝点并不进行定位，默认是false。

    }

    override fun onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，销毁地图
        binding.map.onDestroy();
    }

    override fun onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView.onResume ()，重新绘制加载地图
        binding.map.onResume();
    }

    override fun onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView.onPause ()，暂停地图的绘制
        binding.map.onPause();
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState);
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState)，保存地图当前的状态
        binding.map.onSaveInstanceState(outState);
    }


    override fun initData() {
        vm.signSuccess.observe(this) {
            ToastUtils.showLong("签到成功")
            binding.signStart.text = "签退"
            SPUtils.getInstance().put(Const.PARAM_IS_SIGN, true)
        }

        vm.endSignSuccess.observe(this) {
            ToastUtils.showLong("签退成功")
            binding.signStart.text = "签到"
            SPUtils.getInstance().put(Const.PARAM_IS_SIGN, false)
        }
    }

    override fun initView() {
        binding.signHeader.headerBack.setOnClickListener { finish() }
        binding.signHeader.headerTitle.text = "签到"

//定义了一个地图view

//定义了一个地图view


//初始化地图控制器对象
//初始化地图控制器对象


//初始化定位
//初始化定位
        mLocationClient = AMapLocationClient(applicationContext)
//设置定位回调监听
//设置定位回调监听
        mLocationClient.setLocationListener(mLocationListener)


//初始化AMapLocationClientOption对象
//初始化AMapLocationClientOption对象
        mLocationOption = AMapLocationClientOption()

        mLocationOption.setNeedAddress(true);

        mLocationOption.locationPurpose = AMapLocationClientOption.AMapLocationPurpose.SignIn
        mLocationClient.setLocationOption(mLocationOption);
        //设置场景模式后最好调用一次stop，再调用start以保证场景模式生效
        mLocationClient.stopLocation();
        mLocationClient.startLocation();



        if (SPUtils.getInstance().getBoolean(Const.PARAM_IS_SIGN)) {
            binding.signStart.text = "签退"
        } else {
            binding.signStart.text = "签到"
        }
        binding.signStart.setOnClickListener {
            if (SPUtils.getInstance().getBoolean(Const.PARAM_IS_SIGN)) {
                val map = mutableMapOf<String, Any>()
                map[Const.PARAM_SIGN_END_ADDRESS] = "天津市"
                map[Const.PARAM_SIGN_END_TIME] = System.currentTimeMillis() / 1000
                map[Const.PARAM_SIGN_USER_ID] = SPUtils.getInstance().getInt(Const.PARAM_UID)

                val json = Gson().toJson(map)
                val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
                vm.endSign(body)
            } else {
                val map = mutableMapOf<String, Any>()
                map[Const.PARAM_SIGN_START_ADDRESS] = "天津市"
                map[Const.PARAM_SIGN_START_TIME] = System.currentTimeMillis() / 1000
                map[Const.PARAM_SIGN_USER_ID] = SPUtils.getInstance().getInt(Const.PARAM_UID)

                val json = Gson().toJson(map)
                val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
                vm.sign(body)
            }

        }
    }

}